/*
 * Copyright (c) Huawei Technologies Co., Ltd. 2023-2023. All rights reserved.
 * Description: hiudk sdk adapt
 * Author: ETH group
 * Create: 2023-04-14
 */


#ifndef HIUDK_SDK_HW_COMM_ADPT_H
#define HIUDK_SDK_HW_COMM_ADPT_H

#if defined(HIUDK_SDK_ADPT) && defined(HIUDK_SDK)
#include "hinic3_hw.h"
static inline int sdk_aeq_register_hw_cb(void *udkdev, void *pri_handle,
					 enum hinic3_aeq_type event, hinic3_aeq_hwe_cb hwe_cb)
{
	void *sdk_hwdev = NULL;

	if (!CHECK_UDK_DEV(udkdev)) {
		pr_err("udkdev is error! \n");
		return -EINVAL;
	}

	sdk_hwdev = GET_SDKHWDEV_FROM_HIUDKHWDEV(udkdev);
	return hinic3_aeq_register_hw_cb(sdk_hwdev, pri_handle, event, hwe_cb);
}

static inline int sdk_aeq_register_swe_cb(void *udkdev, void *pri_handle,
            				  enum hinic3_aeq_sw_type event, hinic3_aeq_swe_cb aeq_swe_cb)
{
	void *sdk_hwdev = NULL;

	if (!CHECK_UDK_DEV(udkdev)) {
		pr_err("udkdev is error! \n");
		return -EINVAL;
	}

	sdk_hwdev = GET_SDKHWDEV_FROM_HIUDKHWDEV(udkdev);
	return hinic3_aeq_register_swe_cb(sdk_hwdev, pri_handle, event, aeq_swe_cb);
}

static inline void sdk_aeq_unregister_hw_cb(void *udkdev, enum hinic3_aeq_type event)
{
	void *sdk_hwdev = NULL;

	if (!CHECK_UDK_DEV(udkdev)) {
		pr_err("udkdev is error! \n");
		return;
	}

	sdk_hwdev = GET_SDKHWDEV_FROM_HIUDKHWDEV(udkdev);
	return hinic3_aeq_unregister_hw_cb(sdk_hwdev, event);
}

static inline void sdk_aeq_unregister_swe_cb(void *udkdev, enum hinic3_aeq_sw_type event)
{
	void *sdk_hwdev = NULL;

	if (!CHECK_UDK_DEV(udkdev)) {
		pr_err("udkdev is error! \n");
		return;
	}

	sdk_hwdev = GET_SDKHWDEV_FROM_HIUDKHWDEV(udkdev);
	return hinic3_aeq_unregister_swe_cb(sdk_hwdev, event);
}

static inline struct hinic3_cmd_buf *sdk_alloc_cmd_buf(void *udkdev)
{
	void *sdk_hwdev = NULL;

	if (!CHECK_UDK_DEV(udkdev)) {
		pr_err("udkdev is error! \n");
		return NULL;
	}

	sdk_hwdev = GET_SDKHWDEV_FROM_HIUDKHWDEV(udkdev);
	return hinic3_alloc_cmd_buf(sdk_hwdev);
}

static inline int sdk_alloc_db_addr(void *udkdev, void __iomem **db_base, void __iomem **dwqe_base)
{
	void *sdk_hwdev = NULL;

	if (!CHECK_UDK_DEV(udkdev)) {
		pr_err("udkdev is error! \n");
		return -EINVAL;
	}

	sdk_hwdev = GET_SDKHWDEV_FROM_HIUDKHWDEV(udkdev);
	return hinic3_alloc_db_addr(sdk_hwdev, db_base, dwqe_base);
}

static inline int sdk_alloc_db_phy_addr(void *udkdev, u64 *db_base, u64 *dwqe_base)
{
	void *sdk_hwdev = NULL;

	if (!CHECK_UDK_DEV(udkdev)) {
		pr_err("udkdev is error! \n");
		return -EINVAL;
	}

	sdk_hwdev = GET_SDKHWDEV_FROM_HIUDKHWDEV(udkdev);
	return hinic3_alloc_db_phy_addr(sdk_hwdev, db_base, dwqe_base);
}

static inline int sdk_api_csr_rd64(void *udkdev, u8 dest, u32 addr, u64 *val)
{
	void *sdk_hwdev = NULL;

	if (!CHECK_UDK_DEV(udkdev)) {
		pr_err("udkdev is error! \n");
		return -EINVAL;
	}

	sdk_hwdev = GET_SDKHWDEV_FROM_HIUDKHWDEV(udkdev);
	return hinic3_api_csr_rd64(sdk_hwdev, dest, addr, val);
}

static inline int sdk_ceq_register_cb(void *udkdev, void *pri_handle, enum hinic3_ceq_event event,
				      hinic3_ceq_event_cb callback)
{
	void *sdk_hwdev = NULL;

	if (!CHECK_UDK_DEV(udkdev)) {
		pr_err("udkdev is error! \n");
		return -EINVAL;
	}

	sdk_hwdev = GET_SDKHWDEV_FROM_HIUDKHWDEV(udkdev);
	return hinic3_ceq_register_cb(sdk_hwdev, pri_handle, event, callback);
}

static inline void sdk_ceq_unregister_cb(void *udkdev, enum hinic3_ceq_event event)
{
	void *sdk_hwdev = NULL;

	if (!CHECK_UDK_DEV(udkdev)) {
		pr_err("udkdev is error! \n");
		return;
	}

	sdk_hwdev = GET_SDKHWDEV_FROM_HIUDKHWDEV(udkdev);
	return hinic3_ceq_unregister_cb(sdk_hwdev, event);
}

static inline int sdk_clean_root_ctxt(void *udkdev, u16 channel)
{
	void *sdk_hwdev = NULL;

	if (!CHECK_UDK_DEV(udkdev)) {
		pr_err("udkdev is error! \n");
		return -EINVAL;
	}

	sdk_hwdev = GET_SDKHWDEV_FROM_HIUDKHWDEV(udkdev);
	return hinic3_clean_root_ctxt(sdk_hwdev, channel);
}

static inline int sdk_cmdq_detail_resp(void *udkdev, u8 mod, u8 cmd, struct hinic3_cmd_buf *buf_in,
				       struct hinic3_cmd_buf *buf_out, u64 *out_param, u32 timeout,
				       u16 channel)
{
	void *sdk_hwdev = NULL;

	if (!CHECK_UDK_DEV(udkdev)) {
		pr_err("udkdev is error! \n");
		return -EINVAL;
	}

	sdk_hwdev = GET_SDKHWDEV_FROM_HIUDKHWDEV(udkdev);
	return hinic3_cmdq_detail_resp(sdk_hwdev, mod, cmd, buf_in, buf_out, out_param, timeout, channel);
}

static inline int sdk_cmdq_direct_resp(void *udkdev, u8 mod, u8 cmd, struct hinic3_cmd_buf *buf_in,
				       u64 *out_param, u32 timeout, u16 channel)
{
	void *sdk_hwdev = NULL;

	if (!CHECK_UDK_DEV(udkdev)) {
		pr_err("udkdev is error! \n");
		return -EINVAL;
	}

	sdk_hwdev = GET_SDKHWDEV_FROM_HIUDKHWDEV(udkdev);
	return hinic3_cmdq_direct_resp(sdk_hwdev, mod, cmd, buf_in, out_param, timeout, channel);
}

static inline int sdk_cos_id_detail_resp(void *udkdev, u8 mod, u8 cmd, u8 cos_id,
					 struct hinic3_cmd_buf *buf_in, struct hinic3_cmd_buf *buf_out,
					 u64 *out_param, u32 timeout, u16 channel)
{
	void *sdk_hwdev = NULL;

	if (!CHECK_UDK_DEV(udkdev)) {
		pr_err("udkdev is error! \n");
		return -EINVAL;
	}

	sdk_hwdev = GET_SDKHWDEV_FROM_HIUDKHWDEV(udkdev);
	return hinic3_cos_id_detail_resp(sdk_hwdev, mod, cmd, cos_id, buf_in, buf_out, out_param,
					 timeout, channel);
}

static inline void sdk_event_callback(void *udkdev, struct hinic3_event_info *event)
{
	void *sdk_hwdev = NULL;

	if (!CHECK_UDK_DEV(udkdev)) {
		pr_err("udkdev is error! \n");
		return;
	}

	sdk_hwdev = GET_SDKHWDEV_FROM_HIUDKHWDEV(udkdev);
	return hinic3_event_callback(sdk_hwdev, event);
}

static inline void sdk_free_cmd_buf(void *udkdev, struct hinic3_cmd_buf *cmd_buf)
{
	void *sdk_hwdev = NULL;

	if (!CHECK_UDK_DEV(udkdev)) {
		pr_err("udkdev is error! \n");
		return;
	}

	sdk_hwdev = GET_SDKHWDEV_FROM_HIUDKHWDEV(udkdev);
	return hinic3_free_cmd_buf(sdk_hwdev, cmd_buf);
}

static inline void sdk_free_db_addr(void *udkdev, const void __iomem *db_base, void __iomem *dwqe_base)
{
	void *sdk_hwdev = NULL;

	if (!CHECK_UDK_DEV(udkdev)) {
		pr_err("udkdev is error! \n");
		return;
	}

	sdk_hwdev = GET_SDKHWDEV_FROM_HIUDKHWDEV(udkdev);
	return hinic3_free_db_addr(sdk_hwdev, db_base, dwqe_base);
}

static inline void sdk_free_db_phy_addr(void *udkdev, u64 db_base, u64 dwqe_base)
{
	void *sdk_hwdev = NULL;

	if (!CHECK_UDK_DEV(udkdev)) {
		pr_err("udkdev is error! \n");
		return;
	}

	sdk_hwdev = GET_SDKHWDEV_FROM_HIUDKHWDEV(udkdev);
	return hinic3_free_db_phy_addr(sdk_hwdev, db_base, dwqe_base);
}

static inline int sdk_func_reset(void *udkdev, u16 func_id, u64 reset_flag, u16 channel)
{
	void *sdk_hwdev = NULL;

	if (!CHECK_UDK_DEV(udkdev)) {
		pr_err("udkdev is error! \n");
		return -EINVAL;
	}

	sdk_hwdev = GET_SDKHWDEV_FROM_HIUDKHWDEV(udkdev);
	return hinic3_func_reset(sdk_hwdev, func_id, reset_flag, channel);
}

static inline int sdk_get_board_info(void *udkdev, struct hinic3_board_info *info, u16 channel)
{
	void *sdk_hwdev = NULL;

	if (!CHECK_UDK_DEV(udkdev)) {
		pr_err("udkdev is error! \n");
		return -EINVAL;
	}

	sdk_hwdev = GET_SDKHWDEV_FROM_HIUDKHWDEV(udkdev);
	return hinic3_get_board_info(sdk_hwdev, info, channel);
}

static inline int sdk_get_hw_pf_infos(void *udkdev, struct hinic3_hw_pf_infos *infos, u16 channel)
{
	void *sdk_hwdev = NULL;

	if (!CHECK_UDK_DEV(udkdev)) {
		pr_err("udkdev is error! \n");
		return -EINVAL;
	}

	sdk_hwdev = GET_SDKHWDEV_FROM_HIUDKHWDEV(udkdev);
	return hinic3_get_hw_pf_infos(sdk_hwdev, infos, channel);
}

static inline void *sdk_get_service_adapter(void *udkdev, enum hinic3_service_type type)
{
	void *sdk_hwdev = NULL;

	if (!CHECK_UDK_DEV(udkdev)) {
		pr_err("udkdev is error! \n");
		return NULL;
	}

	sdk_hwdev = GET_SDKHWDEV_FROM_HIUDKHWDEV(udkdev);
	return hinic3_get_service_adapter(sdk_hwdev, type);
}

static inline void sdk_link_event_stats(void *udkdev, u8 link)
{
	void *sdk_hwdev = NULL;

	if (!CHECK_UDK_DEV(udkdev)) {
		pr_err("udkdev is error! \n");
		return;
	}

	sdk_hwdev = GET_SDKHWDEV_FROM_HIUDKHWDEV(udkdev);
	return hinic3_link_event_stats(sdk_hwdev, link);
}

static inline int sdk_mbox_to_pf(void *udkdev, u8 mod, u16 cmd, void *buf_in,
				u16 in_size, void *buf_out, u16 *out_size,
				u32 timeout, u16 channel)
{
	void *sdk_hwdev = NULL;

	if (!CHECK_UDK_DEV(udkdev)) {
		pr_err("udkdev is error! \n");
		return -EINVAL;
	}

	sdk_hwdev = GET_SDKHWDEV_FROM_HIUDKHWDEV(udkdev);
	return hinic3_mbox_to_pf(sdk_hwdev, mod, cmd, buf_in, in_size, buf_out,
				 out_size, timeout, channel);
}

static inline int sdk_mbox_to_vf(void *udkdev, u16 vf_id, u8 mod, u16 cmd,
				void *buf_in, u16 in_size, void *buf_out, u16 *out_size,
             u32 timeout, u16 channel)
{
	void *sdk_hwdev = NULL;

	if (!CHECK_UDK_DEV(udkdev)) {
		pr_err("udkdev is error! \n");
		return -EINVAL;
	}

	sdk_hwdev = GET_SDKHWDEV_FROM_HIUDKHWDEV(udkdev);
	return hinic3_mbox_to_vf(sdk_hwdev, vf_id, mod, cmd, buf_in, in_size,
				buf_out, out_size, timeout, channel);
}

static inline int sdk_mbox_to_vf_no_ack(void *udkdev, u16 vf_id, u8 mod, u16 cmd,
					void *buf_in, u16 in_size, void *buf_out, u16 *out_size, u16 channel)
{
	void *sdk_hwdev = NULL;
 
	if (!CHECK_UDK_DEV(udkdev)) {
		pr_err("udkdev is error! \n");
		return -EINVAL;
	}
 
	sdk_hwdev = GET_SDKHWDEV_FROM_HIUDKHWDEV(udkdev);
	return hinic3_mbox_to_vf_no_ack(sdk_hwdev, vf_id, mod, cmd, buf_in,
				    in_size, buf_out, out_size, channel);
}

static inline int sdk_msg_to_mgmt_api_chain_async(void *udkdev, u8 mod, u16 cmd,
						  const void *buf_in, u16 in_size)
{
	void *sdk_hwdev = NULL;

	if (!CHECK_UDK_DEV(udkdev)) {
		pr_err("udkdev is error! \n");
		return -EINVAL;
	}

	sdk_hwdev = GET_SDKHWDEV_FROM_HIUDKHWDEV(udkdev);
	return hinic3_msg_to_mgmt_api_chain_async(sdk_hwdev, mod, cmd, buf_in, in_size);
}

static inline int sdk_msg_to_mgmt_async(void *udkdev, u8 mod, u16 cmd, void *buf_in,
					u16 in_size, u16 channel)
{
	void *sdk_hwdev = NULL;

	if (!CHECK_UDK_DEV(udkdev)) {
		pr_err("udkdev is error! \n");
		return -EINVAL;
	}

	sdk_hwdev = GET_SDKHWDEV_FROM_HIUDKHWDEV(udkdev);
	return hinic3_msg_to_mgmt_async(sdk_hwdev, mod, cmd, buf_in, in_size, channel);
}

static inline int sdk_msg_to_mgmt_no_ack(void *udkdev, u8 mod, u16 cmd, void *buf_in,
					 u16 in_size, u16 channel)
{
	void *sdk_hwdev = NULL;

	if (!CHECK_UDK_DEV(udkdev)) {
		pr_err("udkdev is error! \n");
		return -EINVAL;
	}

	sdk_hwdev = GET_SDKHWDEV_FROM_HIUDKHWDEV(udkdev);
	return hinic3_msg_to_mgmt_no_ack(sdk_hwdev, mod, cmd, buf_in, in_size, channel);
}

#endif
#endif
